package com.zuqiuzhandui.liveing.weight;

import android.app.Activity;
import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.DownloadListener;
import android.widget.TextView;

import com.code19.library.L;
import com.zuqiuzhandui.R;

/**
 * Created by yq on 2016-10-12.
 */

public class CountDownView extends TextView{
    CountDownTimer timer;
    final String TAG="countDownView";
    int time;
    int i;
    countDownListener downListener;
    public CountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        setGravity(Gravity.CENTER);
        setTextSize(80);
        setTextColor(ContextCompat.getColor(getContext(),R.color.white));
    }


    public void startCountDown(int time, countDownListener downListener){
        this.time=time+1;
//            setText(""+second);
        this.downListener=downListener;
        mHandler.obtainMessage(0x11).sendToTarget();

    }

    Handler mHandler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what)
            {
                case 0x11:
                    setVisibility(VISIBLE);
                    timer   = new CountDownTimer(time * 1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            L.d(TAG,"开始运行第"+i+++"次"+System.currentTimeMillis());
                            L.d(TAG,"剩余时间"+millisUntilFinished/1000);
                            setText(""+--time);
                            startAnimotion();
                            if (millisUntilFinished/1000<=1)
                            {
                                mHandler.sendEmptyMessageDelayed(0x12,1000);
                            }
                        }

                        @Override
                        public void onFinish() {
                            L.d(TAG,"在这个地方停止了"+System.currentTimeMillis());
                            stopCountDown();
                        }
                    };
                    timer.start();
                    break;
                case 0x12:
                    setText("开始");
                    break;
            }

        }
    };
    private void stopCountDown()
    {
        clearAnimation();
        setVisibility(GONE);
        downListener.showCountDown();
    }

    private void startAnimotion() {
        Animation countDownAnimation= AnimationUtils.loadAnimation(getContext(), R.anim.count_down_animation);
        countDownAnimation.setDuration(1000);
        startAnimation(countDownAnimation);
    }

    private void cancelTimer(){
        timer.cancel();
        setVisibility(GONE);
    }

    public interface countDownListener{
        void showCountDown();
    }


}
